﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This filter is used to reduce noise in a bitmap without blurring
    /// </summary>
    public class Sigma : BaseImageFilter
    {
        /// <summary>
        /// Sigma
        /// </summary>
        protected byte m_nSigma = 100;
        protected int m_nSize = 11;

        /// <summary>
        /// Initializes a new instance of the <see cref="Sigma"/> class.
        /// </summary>
        public Sigma()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Sigma"/> class.
        /// </summary>
        /// <param name="_sigma">The _sigma.</param>
        public Sigma(int _nSize, byte _sigma)
        {
            m_nSize = _nSize;
            m_nSigma = _sigma;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Sigma"/> class.
        /// </summary>
        /// <param name="_sigma">The _sigma.</param>
        internal Sigma(Sigma _sigma)
        {
            this.m_nSigma = _sigma.m_nSigma;
        }

        /// <summary>
        /// Gets or sets the sigma window.
        /// </summary>
        /// <value>The sigma window.</value>
        public byte SigmaValue
        {
            get { return m_nSigma; }
            set { m_nSigma = value; }
        }

        /// <summary>
        /// Gets or sets the sigma window.
        /// </summary>
        /// <value>The sigma window.</value>
        public int WindowSize
        {
            get { return m_nSize; }
            set { m_nSize = value; }
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmapCloned = (UnsafeBitmap)_bitmap.Clone();
            Int32 x, xx, xxx, y, yy, yyy;
            Int32 r, g, b, sumR, sumG, sumB, countR, countG, countB;
            Color color = Color.Transparent;
            float Rf, Gf, Bf;
            int Nh = m_nSize/2;

            int nHeight = _bitmap.Height;
            int nWidth = _bitmap.Width;

            for (y = Nh; y < nHeight - Nh; y++) //==================
            {
                for (x = Nh; x < nWidth - Nh; x++) //===============
                {
                    color = bitmapCloned.GetPixel(x, y);
                    sumR = sumG = sumB = countR = countG = countB = 0;
                    for (yy = -Nh; yy <= Nh; yy++) //=============
                    {
                        yyy = y + yy;
                        for (xx = -Nh; xx <= Nh; xx++)//========
                        {
                            xxx = x + xx; r = color.R; g = color.G; b = color.B;
                            if (Math.Abs(color.R - r) <= m_nSigma) { sumR += r; countR++; }
                            if (Math.Abs(color.G - g) <= m_nSigma) { sumG += g; countG++; }
                            if (Math.Abs(color.B - b) <= m_nSigma) { sumB += b; countB++; }
                        } //====== end for (int xx... ================
                    } //======== end for (int yy... ==================
                    Rf = sumR / countR; Gf = sumG / countG; Bf = sumB / countB;
                    _bitmap.SetPixel(x, y, Color.FromArgb(Convert.ToInt32(Rf),
                                                       Convert.ToInt32(Gf),
                                                       Convert.ToInt32(Bf)));
                } //============ end for (int x... =====================
            } //============== end for (int y... =======================
            //return true;
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Sigma(this);
        }
    }
}
